package com.h3bpm.kumhosunny.crm.controller.budget;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import com.h3bpm.kumhosunny.crm.controller.enums.DateEnum;
import data.DataRow;
import data.DataTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 预算外申请
 * @authormi
 * @date2019/9/11
 */
@Controller
@RequestMapping("/Portal/CRM/BudgetBeyondApply")
public class BudgetBeyondApply  extends ControllerBase {


    @Override
    public String getFunctionCode() {
        return null;
    }


    /**
     * 流程审批结束增加预算外申请金额跟预算金额合并
     * @param instanceId
     * @return
     */
    @RequestMapping("/addApplyBudget")
    @ResponseBody
   public JSONObject addApplyBudget(String instanceId){

        JSONObject result = new JSONObject();
       result.put("status","200");
       result.put("message","额外申请预算加入可用预算成功");
       SimpleDateFormat simpleDateFormatUpdate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       try {
           InstanceData instanceData = new InstanceData(getEngine(),instanceId, User.AdministratorID);
           //预算外申请年月获取
           String year =  instanceData.getBizObject().GetValue("applYear").toString();
           String month = instanceData.getBizObject().GetValue("applMonth").toString();
           //获取申请人
           String userId= instanceData.getBizObject().getCreatedBy();

           String getDubgetSql = "select * from k_dubgetexpand where userId = '"+userId+"' and byear='"+year+"' and  bmonth='"+month+"'";
           DataTable dataDubget =  SqlUtil.doQuery(getDubgetSql);
           if(dataDubget!=null && dataDubget.getEntityRows()!=null && dataDubget.getEntityRows().size()>0){
               DataRow dataRow = dataDubget.getEntityRows().get(0);
               //获取可用预算
               BigDecimal availableBudget = dataRow.getBigDecimal("availableBudget");
               //获取额外申请预算
               BigDecimal applicationBudget  = dataRow.getBigDecimal("applicationBudget");
               //获取申请额度
               BigDecimal applyMoney =  new BigDecimal(instanceData.getBizObject().GetValue("applBudget").toString()) ;
               //可用预算增加申请额度
               availableBudget =  availableBudget.add(applyMoney);
               //额外申请预算额度加上申请金额额度
               applicationBudget = applicationBudget.add(applyMoney);

               //预算拓展表更新
               String updateDubgetSql = "update k_dubgetexpand set  ModifiedBy = '"+userId+"' ,ModifiedTime='"+simpleDateFormatUpdate.format(new Date())+"' , applicationBudget ='"+applicationBudget+"' , availableBudget= '"+availableBudget+"' where objectId ='"+dataRow.getString("objectId")+"'";
               SqlUtil.doNoQuery(updateDubgetSql);

               //增加额外预算申请明细表记录
               String objectId = UUID.randomUUID().toString();
               String kBudgetAppSchInsertSql = " insert into  k_budget_beyond_apply (objectId,applyUserId,applyMoney,applyTime,status,CreatedTime,CreatedBy,ModifiedTime,ModifiedBy,applicationBudgetInstanceId) values ";
               String kBudgetAppSchInsertValue="('"+objectId+"','"+userId+"','"+applyMoney+"','"+year+"-"+month+"','4','"+simpleDateFormatUpdate.format(new Date())+"','"+userId+"','"+simpleDateFormatUpdate.format(new Date())+"','"+userId+"','"+instanceId+"')";
               SqlUtil.doNoQuery(kBudgetAppSchInsertSql+kBudgetAppSchInsertValue);
               BudgetSumSoloController.updatePracticalAvailableBudget(year+"-"+month,userId);
               result.put("status","200");
               result.put("message","额外申请预算加入可用预算成功");
               return result;
           }else{
               result.put("status","500");
               result.put("message","当前申请人没有可用预算记录，请导入此人预算");
               return result;
           }
       } catch (Exception e) {
           result.put("status","500");
           result.put("message","系统异常");
           e.printStackTrace();
       }


       return result;
   }


    /**
     * 获取预算额外申请累计金额
     * @param applYear
     * @param applMonth
     * @param userId
     * @return
     */
    @RequestMapping("/getApplyAddBudgetMoney")
    @ResponseBody
   public JSONObject getApplyAddBudgetMoney(String applYear,String applMonth , String userId){
        JSONObject result = new JSONObject();
        result.put("status","200");
        result.put("message","成功");
        try{
            String getDubgetSql = "select * from k_dubgetexpand where userId = '"+userId+"' and byear='"+applYear+"' and  bmonth='"+applMonth+"'";
            DataTable dataTable =  SqlUtil.doQuery(getDubgetSql);
            DataRow  dataRow =  dataTable.getEntityRows().get(0);
            result.put("status","200");
            result.put("message","成功");
            result.put("data",dataRow.getString("applicationBudget"));
            return result;
        }catch (Exception e){
            result.put("status","500");
            result.put("message","系统异常");
            e.printStackTrace();
        }
        return result;
   }

     @RequestMapping("/getBudgetMoney")
     @ResponseBody
     public JSONObject getBudgetMoney(String applYear,String applMonth , String userId){
         JSONObject result = new JSONObject();
         result.put("status","200");
         result.put("message","成功");
         try{
             String getDubgetSql = "select * from k_budget where userId = '"+userId+"' and belongToYear='"+applYear+"' and  belongToMonth='"+applMonth+"'";
             DataTable dataTable =  SqlUtil.doQuery(getDubgetSql);
             DataRow  dataRow =  dataTable.getEntityRows().get(0);
             result.put("status","200");
             result.put("message","成功");
             result.put("data",dataRow.getString("monthBudget"));
             return result;
         }catch (Exception e){
             result.put("status","500");
             result.put("message","系统异常");
             e.printStackTrace();
         }
         return result;
     }

}
